﻿using Ln.Fram.SqlSugarCore.Abstractions;
using Ln.Rbac.Domain.Entities;
using Ln.Rbac.Domain.Entities.ValueObjects;
using Ln.Rbac.Domain.Shared.Enums;
using Ln.Rbac.Domain.Shared.Options;
using Microsoft.Extensions.Options;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;

namespace Ln.Rbac.SqlSugarCore.DataSeeds;

public class UserDataSeed : IDataSeedContributor, ITransientDependency
{
	#region 构造函数
	public UserDataSeed(ISqlSugarRepository<UserAggregateRoot> repository, IOptions<RbacOptions> options) => (_repository, _options) = (repository, options.Value);
	#endregion

	#region 成员属性
	private ISqlSugarRepository<UserAggregateRoot> _repository;
	private RbacOptions _options;
	#endregion

	#region 自身方法
	public async Task SeedAsync(DataSeedContext context) { if (!await _repository.IsAnyAsync(x => true)) await _repository.InsertManyAsync(GetSeedData()); }

	private List<UserAggregateRoot> GetSeedData()
	{
		var entities = new List<UserAggregateRoot>();
		entities.AddRange([
			new() { Name = "管理员", UserName = "Admin", Nick = "管理员", EncryPassword = new EncryPasswordValueObject(_options.AdminPassword), Email = "nwsystem@newwaymask.net", OrderNum = 999, IsDeleted = false, State = true },
			new() { Name = "测试用户", UserName = "Test", Nick = "测试用户", EncryPassword = new EncryPasswordValueObject("Test.123456"), Email = "nwtest@newwaymask.net", OrderNum = 998, IsDeleted = false, State = true },
		]);
		foreach (var item in entities)
			item.BuildPassword();
		return entities;
	}
	#endregion
}
